package com.ido.otaclient.device;



import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ido.otaclient.DeviceManagerModel;
import com.ido.otaclient.R;
import com.ido.otaclient.base.BaseFragment;
import com.ido.otaclient.base.IDLog;
import com.ido.otaclient.module.Veneer;
import com.ido.otaclient.util.ResourceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-02-11 17:13
 * @description
 */
public class DeviceFragment extends BaseFragment {
    private static final String TAG = "DeviceFragment";
    /**
     * 设备数量
     */
    @BindView(R.id.tv_device_account)
    TextView mTvDeviceAccount;

    /**
     * 单板数量
     * @return
     */
    @BindView(R.id.tv_veneer_account)
    TextView mTvDeviceVeneer;
    /**
     * 单板数量
     */
    @BindView(R.id.rv_veneer)
    RecyclerView mRvVeneer;

    /**
     * 设备列表
     */
    private List<Veneer> mVeneerList = new ArrayList<>();

    /**
     * 适配器
     */
    private DeviceAdapter mDeviceAdapter;

    private DeviceManagerModel mManagerModel;

    public static DeviceFragment newInstance() {
        
        Bundle args = new Bundle();
        
        DeviceFragment fragment = new DeviceFragment();
        fragment.setArguments(args);
        return fragment;
    }




    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_device;
    }

    @Override
    public void onCreateTask(Bundle savedInstanceState) {
        super.onCreateTask(savedInstanceState);

    }

    @Override
    public void onViewCreatedTask(View view, Bundle savedInstanceState) {
        super.onViewCreatedTask(view, savedInstanceState);
        initData();
    }

    private void initData() {

        mManagerModel = ViewModelProviders.of(getActivity()).get(DeviceManagerModel.class);

        mManagerModel.getVeneerModelLiveData().observe(getActivity(),veneerList -> update(veneerList));
        
        mManagerModel.getDeviceCout().observe(this,count -> showDeviceCount(count));


        //设置LayoutManager为LinearLayoutManager
        mRvVeneer.setLayoutManager(new GridLayoutManager(getActivity(),3));
        //设置Adapter
        mDeviceAdapter = new DeviceAdapter(getActivity(),mVeneerList);
        mRvVeneer.setAdapter(mDeviceAdapter);
    }

    /**
     * 显示发现设备
     */
    private void showDeviceCount(int count) {
        if(count == 0){
            mTvDeviceAccount.setTextColor(ResourceUtil.getColor(R.color.com_color_red));
            mTvDeviceAccount.setText(String.valueOf(count));
        }else {
            mTvDeviceAccount.setText(String.valueOf(count));
            mTvDeviceAccount.setTextColor(ResourceUtil.getColor(R.color.com_color_black_text));
        }

    }


    @Override
    public void onStartTask() {
        super.onStartTask();
        //更新单板状态
       // showDeviceInfo();
    }

    @Override
    public void onResumeTask() {
        super.onResumeTask();
        //showDeviceInfo();
    }

    /**
     * 显示连接设备信息
     */
    private void showDeviceInfo() {
        IDLog.d(TAG, "showDeviceInfo: ");
        /**
         * 得到设备信息显示
         */
        mVeneerList = mManagerModel.getVeneerModelLiveData().getValue();
        List<Veneer> veneerList = new ArrayList<>();
        if(mVeneerList != null){
            IDLog.d(TAG, "showDeviceInfo: " + mVeneerList.size());
            veneerList.addAll(mVeneerList);
            update(veneerList);
            for(int i = 0;i < mVeneerList.size();i++){
                IDLog.d(TAG, "showDeviceInfo: " + mVeneerList.get(i).toString());
            }
        }else {
            mVeneerList = new ArrayList<>();
        }
    }

    /**
     * 更新单板的连接数量和状态
     * @param veneerList
     */
    private void update(List<Veneer> veneerList) {
        IDLog.d(TAG, "update: " + veneerList);
        if(mVeneerList != null)
        mVeneerList.clear();
        mVeneerList.addAll(veneerList);
        mDeviceAdapter.setData(mVeneerList);
        //mDeviceAdapter.notifyDataSetChanged();
        int countEffect = mVeneerList.size();
        for(Veneer veneer : mVeneerList){
            if(veneer.getFailConnectCount() > 3){
                countEffect--;
            }
        }
        if(countEffect == 0){
            mTvDeviceVeneer.setTextColor(ResourceUtil.getColor(R.color.com_color_red));
            mTvDeviceVeneer.setText(String.valueOf(0));
        }else {
            mTvDeviceVeneer.setText(String.valueOf(veneerList.size()));
            mTvDeviceVeneer.setTextColor(ResourceUtil.getColor(R.color.com_color_black_text));
        }
    }

}
